<?php


namespace app\admin\model;


use app\common\model\TimeModel;
use app\wechat\model\Login;
use think\Model;

class UserBase extends TimeModel
{

    public static function onBeforeWrite(Model $model)
    {
        if ($model->nickname) {
            $model->nickname = base64_encode($model->nickname);
        }
    }

    public static function onAfterRead(Model $model)
    {
        //昵称转义
        if (isset($model->nickname)) {
            $model->nickname = base64_decode($model->nickname);
        }
        //客户端类型
        if (isset($model->client_type)) {
            $model->client_name = ($model->client_type == 1) ? '微信' : '支付宝';
        }
        //权限类型
        if (isset($model->user_author)) {
            $author_arr = ['1' => '消费者', '2' => '商家', '3' => '银行职员'];
            $model->author_list = str_replace(array_keys($author_arr), array_values($author_arr), $model->user_author);
        }
    }

    public function getInfoByOpenId($openid)
    {
        return $this->where(['openid' => $openid])->field(['id', 'openid', 'has_money1'])->find();
    }

    public function getInfoByPhone($phone)
    {//弃用，手机号找人不准确
        return $this->where(['mobile' => $phone])->field(['id', 'openid', 'has_money1'])->find();
    }

    /**
     * @title 根据微信openid自动注册
     * @param $openid
     * @param array $ex
     * @return int|string
     * @author FashionJune
     */
    public function addUserByOpenId($openid, $ex = [])
    {
        $add_data = [
            'openid' => $openid,
            'create_time' => time(),
            'update_time' => time(),
        ];
        foreach ($ex as $key => $val) {
            if ($key == 'nickname') {
                $val = base64_encode($val);
            }
            $add_data[$key] = $val;
        }
        try {
            $user_id = $this->insertGetId($add_data);
        } catch (\Exception $e) {
            SystemErrorLog::init()->addError($e->getMessage());
        }
        return $user_id ?? -1;
    }

    public function updateUserByOpenId($openid, $userinfo)
    {
        return $this->where(['openid' => $openid])->find()->save($userinfo);
    }

    public function bindAdmin($user_id, $admin_id)
    {
        $old_bind_user = $this->where('bind_admin', $admin_id)->find();
        if($old_bind_user){
            $old_bind_author = str2arr($old_bind_user['user_author']);
            if (($key = array_search('3', $old_bind_author))) {
                unset($old_bind_author[$key]);
            }
            $old_bind_user->save([
                'bind_admin' => 0,
                'user_author'=> arr2str($old_bind_author)
            ]);
        }

        $bind_user = $this->where('id', $user_id)->find();
        if (!$bind_user) {
            return ;
        }
        $bind_author = str2arr($bind_user['user_author']);
        array_push($bind_author, 3);
        $bind_user->save([
            'bind_admin' => $admin_id,
            'user_author' => arr2str($bind_author)
        ]);
    }

    public function bindStore($user_id, $store_id)
    {
        $old_bind_user = $this->where('bind_store', 'find in set', $store_id)->find();
        if($old_bind_user){
            //清除绑定店铺
            $store_list = str2arr($old_bind_user['bind_store']);
            if ($key = array_search($store_id, $store_list)) {
                unset($store_list[$key]);
            }
            $old_bind_author = str2arr($old_bind_user['user_author']);
            if (!$store_list) {//失去所有店铺绑定，失去店主身份
                if (($key = array_search('2', $old_bind_author))) {
                    unset($old_bind_author[$key]);
                }
            }
            $old_bind_user->save([
                'bind_store' => arr2str($store_list),
                'user_author'=> arr2str($old_bind_author)
            ]);
        }
        $bind_user = $this->where('id', $user_id)->find();
        if ($bind_user) {
            $bind_author = str2arr($bind_user['user_author']);
            if (!array_search('2', $bind_author)) {
                array_push($bind_author, 2);
            }
            $store_list = str2arr($bind_user['bind_store']);
            if (!in_array($store_id, $store_list)) {
                $store_list[] = $store_id;
            }
            $bind_user->save([
                'bind_store' => arr2str($store_list),
                'user_author' => arr2str($bind_author)
            ]);
        }
    }


    public function getBindAdmin($user_id)
    {
        return $this->where('id', $user_id)->cache(10)->value('bind_admin') ?? 0;
    }


    public function getAppletLoinTips($user)
    {
        $user_id = $user['id'];
        //预付金是否充足
        $min_money = sysconfig('rule', 'rule_charge_max');
        $money_tips = ['type' => 'money',];
        if ($min_money > $user['has_money1']) {
            $money_tips['alert'] = true;
            $money_tips['msg'] = '请保证足够的预充值金额';
        } else {
            $money_tips['alert'] = false;
        }
        //是否有未完成的支付
        $recharge_map = [
            'user_id' => $user_id,
            'status' => 1,
        ];
        $recharge_tips = ['type' => 'recharge'];
        $order_sn = UserRecharge::where($recharge_map)->value('order_sn');
        if ($order_sn) {
            $recharge_tips['alert'] = true;
            $recharge_tips['msg'] = '您有一条待支付的充值记录';
            $recharge_tips['order_sn'] = $order_sn;
        } else {
            $recharge_tips['alert'] = false;
        }
        //是否有未完成订单
        $order_map = [
            'user_id' => $user_id,
            'status' => [1, 2],
        ];
        $order_tips = ['type' => 'order'];
        $order_sn = ProductOrder::where($order_map)->value('order_sn');
        if ($order_sn) {
            $order_tips['alert'] = true;
            $order_tips['msg'] = '您有一条进行中的订单记录';
            $recharge_tips['order_sn'] = $order_sn;
        } else {
            $order_tips['alert'] = false;
        }
        return [$money_tips, $recharge_tips, $order_tips];
    }


}